package com.hkj.shop.base.dao;


import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.hkj.shop.base.model.BaseEntity;

public class BaseDao {
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
    public Session getSession() {
		return this.sessionFactory.openSession();
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public boolean save(BaseEntity entity) {
		Session session = null;
		try {
			session = this.getSession();
		    session.save(entity);
		} catch (Exception e) {
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	public boolean update(BaseEntity entity) {
		Session session = null;
		try {
			session = this.getSession();
			session.update(entity);
		} catch (Exception e) {
			return false;
		} finally {
			session.close();
		}
		return true;	
	}
	
	public boolean delete(BaseEntity entity) {
		Session session = null;
		try {
			session = this.getSession();
			session.delete(entity);
		} catch (Exception e) {
			return false;
		} finally {
			session.close();
		}
		return true;
	}
}
